/*
 * $Id: IconRenderer.java,v 1.2 2003/05/20 21:06:53 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.gui;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ImageIcon;

import java.awt.Component;
import net.sf.vofce.export.Template;

/**
 * List cell renderer that renders a list of templates with
 * appropriate icons.
 *
 * @author Tomas Znamenacek
 */
public class IconRenderer extends JLabel implements ListCellRenderer {

	/** The icons used in list. */
	protected ImageIcon[] icons;
	
	/** Creates the cell renderer, loads the icons. */
	public IconRenderer() {
		setOpaque(true);
		setHorizontalAlignment(LEFT);
		setVerticalAlignment(CENTER);

		icons = new ImageIcon[3];
		
		icons[0] = getIcon("user.gif");
		icons[1] = getIcon("system.gif");
		icons[2] = getIcon("internal.gif");
	}

	/** Returns the cell renderer. */
	public Component getListCellRendererComponent(JList list, Object value, int index,
		boolean isSelected, boolean cellHasFocus) {
        
		if (isSelected) {
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
		} else {
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}

		Template temp = (Template) value;
		
		if (temp.getType() == Template.TT_USER) {
			setIcon(icons[0]);
		}
		
		if (temp.getType() == Template.TT_SYSTEM) {
			setIcon(icons[1]);
		}

		if (temp.getType() == Template.TT_INTERNAL) {
			setIcon(icons[2]);
		}

		// s tim paddingem to delam
		// nerad, co se da delat :)
		setText(temp.toString() + "    ");
		
		setFont(list.getFont());
		return this;
	}
	
	/**
	 * Returns image associated with given resource id.
	 */
	private ImageIcon getIcon(String id) {

		return new ImageIcon(getClass().getResource("/" + id));
	}
}
